package com.pason.onlineshop.mainshop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pason.onlineshop.dist.shopdata.IShopDataService;
import com.pason.onlineshop.dist.shopdata.model.Product;


@Controller
public class ProductController {

	private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
	
	@Autowired(required=true)
	private IShopDataService shopDataService;
	
	@RequestMapping(value = "/listProducts", method = RequestMethod.GET)
	public String listProducts(final HttpServletRequest request, Model model) {
		List<Product> products = shopDataService.getShopData().getProducts();
		logger.info("------Products["+products.size()+"]-----------");
		model.addAttribute("products", products);
		return "listProducts";
	}
	
	@RequestMapping(value = "/listProducts2", method = RequestMethod.GET)
	@ResponseBody
	public List<Product> listProducts2(final HttpServletRequest request, Model model) {
		List<Product> products = shopDataService.getShopData().getProducts();
		logger.info("------Products2["+products.size()+"]-----------");
		return products;
	}
}
